// polyfill
import 'babel-polyfill'
// Vue
import Vue from 'vue'
import App from './App'
// store
import store from '@/store/index'
// 模拟数据
// import '@/mock'
// 多国语
import i18n from './i18n'
// 核心插件
import d2Admin from '@/plugin/d2admin'
// 路由
import router from './router'
// 全局filter
import * as filters from './filters'
// [ 可选插件 ] 数据导入导出
import pluginExport from '@d2-projects/vue-table-export'
import pluginImport from '@d2-projects/vue-table-import'

import { mapMutations, mapActions } from 'vuex'

import * as bookingService from '@/api/booking'
import * as clientService from '@/api/customer'

import vueAudioNative from 'vue-audio-native'

// 核心插件
Vue.use(d2Admin, { store })

Vue.use(pluginExport)
Vue.use(pluginImport)
Vue.use(vueAudioNative)

// 全局filter
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

new Vue({
  data: {
    dialogArr: []
  },
  router,
  store,
  i18n,
  render: h => h(App),
  created () {
    // setInterval(() => {
    //   this.getBookingData()
    // }, 20000)
  },
  mounted () {
    // 展示系统信息
    this.$store.commit('d2admin/releases/versionShow')
    // 用户登录后从数据库加载一系列的设置
    this.$store.dispatch('d2admin/account/load')
    // 获取并记录用户 UA
    this.$store.commit('d2admin/ua/get')
    // 初始化全屏监听
    this.$store.dispatch('d2admin/fullscreen/listen')
  },
  methods: {
    ...mapMutations('d2admin/page', [
      'keepAliveRemove',
      'keepAliveClean'
    ]),
    ...mapActions('d2admin/page', [
      'close',
      'closeLeft',
      'closeRight',
      'closeOther',
      'closeAll'
    ]),
    getBookingData () {
      for (var i = 0; i < this.dialogArr.length; i++) {
        this.dialogArr[i].close()
      }
      this.dialogArr = []
      let _this = this
      bookingService.getUnReadList().then(data => {
        if (data.result.length > 0) {
          let records = data.result
          const h = this.$createElement
          records.forEach(function (item, idx) {
            setTimeout(() => {
              _this.dialogArr.push(_this.$notify.info({
                title: '预约提醒',
                position: 'bottom-right',
                duration: 0,
                message: h('p', null, [
                  h('span', null, item.bookingTime + '   '),
                  h('button', {
                    on: {
                      click: _this.clickBtn
                    },
                    attrs: {
                      id: item.id,
                      idx: idx,
                      clientId: item.clientId
                    }
                  }, '详情')
                ])
              }))
            }, 1000)
          })
        }
      })
    },
    clickBtn (event) {
      let _this = this
      let idx = event.target.getAttribute('idx')
      let id = event.target.getAttribute('id')
      let clientId = event.target.getAttribute('clientId')
      this.dialogArr[idx].close()
      bookingService.changeStatusById(id)
      clientService.getObj(clientId).then(response => {
        _this.$router.push({
          name: 'detail',
          query: { id: clientId, clientName: response.clientName, obj: response }
        })
        // _this.$nextTick(_this.$router.replace('/refresh'))
      })
    }
  },
  watch: {
    // '$route.matched'(val) {
    //   let fullAside = this.$store.state.d2admin.menu.fullAside
    //   const _side = fullAside.filter(menu => menu.path === val[0].path)
    //   this.$store.commit('d2admin/menu/asideSet', _side.length > 0 ? _side[0].children : [])
    // }
  }
}).$mount('#app')
